﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static WinFormsApp1.WangHaoQi._02Winfroms.OilyfatShow;
using WinFormsApp1.Common;

namespace WinFormsApp1.WangHaoQi._02Winfroms
{
    public partial class ScratchesShow : Form
    {
        public ScratchesShow()
        {
            InitializeComponent();
            GetProduction();
        }

        public class Scratchess
        {
            public int pageIndex { get; set; }

            public int pageSize { get; set; }

            public string ScratchesDate { get; set; }

            public string ScratchesDocument { get; set; }

            public string ScratchesSlaughter { get; set; }

            public int ProductionId { get; set; }
        };


        public int pageIndex = 1;

        public int pageSize = 3;

        public int totalcount = 0;

        public int pagecount = 0;

        public string ScratchesDate = "";

        public string ScratchesDocument = "";

        public string ScratchesSlaughter = "";

        public int ProductionId = 0;

        private async void ScratchesShow_Load(object sender, EventArgs e)
        {
            var clients = new Scratchess
            {
                pageIndex = pageIndex,
                pageSize = pageSize,
                ScratchesDate= ScratchesDate,
                ScratchesDocument= ScratchesDocument,
                ScratchesSlaughter= ScratchesSlaughter,
                ProductionId=ProductionId,
            };

            var items = await HttpClientHelper.RequestUrl<object, ApiResultFY1<List<Scratcheses>>>(URLCommon.Url2, "/WeekListOne_Leo/GeOScratchesShow", HttpClientHelper.MethodType.POST, clients);
            this.dataGridView1.DataSource = items.Data.list;
            totalcount = items.Data.totalcount;
            pagecount = items.Data.pagecount;

            label1.Text = "共" + totalcount + "条,每页" + pageSize + "条，当前第" + pageIndex + "页，共" + pagecount + "页";
        }

        public class Scratcheses
        {
            /// <summary>
            /// 膘油称重Id
            /// </summary>
            public int ScratchesId { get; set; }

            /// <summary>
            /// 单据日期
            /// </summary>
            public DateTime ScratchesDate { get; set; }

            /// <summary>
            /// 单据号
            /// </summary>
            public string ScratchesDocument { get; set; }

            /// <summary>
            /// 生产摘要
            /// </summary>
            public string ScratchesSummary { get; set; }

            /// <summary>
            /// 屠宰计划
            /// </summary>
            public string ScratchesSlaughter { get; set; }

            /// <summary>
            /// 生产班组
            /// </summary>
            public string ProductionName { get; set; }

            /// <summary>
            /// 审核状态
            /// </summary>
            public bool ScratchesState { get; set; }

            /// <summary>
            /// 审核人
            /// </summary>
            public string ScratchesReviewers { get; set; }

            /// <summary>
            /// 审核时间
            /// </summary>
            public DateTime ScratchesTime { get; set; }

            /// <summary>
            /// 制单人
            /// </summary>
            public string MakeName { get; set; }

            /// <summary>
            /// 制单时间
            /// </summary>
            public DateTime MakeTime { get; set; }

            /// <summary>
            /// 最后修改时间
            /// </summary>
            public DateTime MakeUpdateTime { get; set; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (pageIndex != 1)
            {
                pageIndex = 1;
            }
            ScratchesShow_Load(sender, e);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (pageIndex > 1)
            {
                pageIndex--;
            }
            else
            {
                //弹框提示
                MessageBox.Show("已经第一页了");
            }
            ScratchesShow_Load(sender, e);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (pageIndex < pagecount)
            {
                pageIndex++;
            }
            else
            {
                //弹框提示
                MessageBox.Show("已经最后一页了");
            }
            ScratchesShow_Load(sender, e);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (pageIndex != pagecount)
            {
                pageIndex = pagecount;
            }
            ScratchesShow_Load(sender, e);
        }

        public record Scratcheter(int ScratchesId);

        private async void dataGridView1_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            if (MessageBox.Show("您确定要删除吗？", "此删除为逻辑删除", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
            {
                if (e.ColumnIndex == dataGridView1.Columns["删除"].Index)
                {
                    var wins = (int)e.RowIndex;
                    if (wins >= 0)
                    {
                        int ScratchesId = Convert.ToInt32(dataGridView1.Rows[wins].Cells["序号"].Value);

                        Scratcheter scratcheter = new(ScratchesId);

                        var items = await HttpClientHelper.RequestUrl<Scratcheter, ApiResult<List<Scratcheses>>>(URLCommon.Url1, "/WorkList01_leo/GetScratchesDel", HttpClientHelper.MethodType.POST, scratcheter);

                        if (items.Code == 1)
                        {
                            MessageBox.Show("删除成功");
                        }
                        else
                        {
                            MessageBox.Show("删除失败");
                        }


                    }
                }
            }
        }


        private void dataGridView1_CellFormatting_1(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns["审核状态"].Index && e.Value != null)
            {
                if (e.Value.ToString() == "True")
                {
                    e.Value = "未通过";
                    e.FormattingApplied = true;
                }
                else
                {
                    e.Value = "已通过";
                    e.FormattingApplied = false;
                }
            }
        }

        //班组
        public async void GetProduction()
        {
            var items = await HttpClientHelper.RequestUrl<string, ApiResult<List<Productions>>>(URLCommon.Url2, "/WeekListOne_Leo/GetProduction", HttpClientHelper.MethodType.GET, default);

            uiComboBox1.DataSource = items.Data;
            uiComboBox1.DisplayMember = "ProductionName";
            uiComboBox1.ValueMember = "ProductionId";

            this.Controls.Add(uiComboBox1);
        }

        public class Productions
        {
            public int ProductionId { get; set; }

            /// <summary>
            /// 生产班组名称
            /// </summary>
            public string ProductionName { get; set; }
        }

        //查询
        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (uiDatePicker1.Text != null)
            {
                ScratchesDate =Convert.ToString(uiDatePicker1.Text);
            }
            if (uiTextBox2.Text != null)
            {
                ScratchesDocument = Convert.ToString(uiTextBox2.Text);
            }
            if (uiTextBox3.Text != null)
            {
                ScratchesSlaughter = Convert.ToString(uiTextBox3.Text);
            }
            if (uiComboBox1.SelectedValue != null)
            {
                ProductionId = Convert.ToInt32(uiComboBox1.SelectedValue);
            }
            ScratchesShow_Load(sender, e);
        }
    }
}
